<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

/*
    Function : e107shop_manage_settings
    --------------------------------------------------
    Parameters :    $action     The action to take
                    $cat        The category
    ==================================================
    This function displays the form for adding/editing
    a product.
    ==================================================
*/

function e107shop_manage_settings($action, $cat) {
    global $e107shop_db_name, $sql, $ns;
    if($action==1 || $sql->db_Select($e107shop_db_name['settings'], "*", "")) {
        //
        //edit the product
        //
        $sql->db_Select($e107shop_db_name['settings'], "*", "");
        while($row=$sql->db_Fetch()) {
            //
            // Store the fields in variables so they can be
            // used in the form, without having to write out the
            // form twice.
            //
            $store_name = $row["store_name"];
            $store_language = $row["language"];
            $order_email = $row["order_email"];
            $use_coupons = $row["use_coupons"];
            $store_currency = $row["currency"];
            $currency_symbol = $row["currency_symbol"];
            $store_order_id = $row["orderID"];
            $item_display_order = $row["item_display_order"];
            $products_per_page = $row["products_per_page"];
            $category_display = $row["category_display"];
            $customer_member = $row["customer_must_be_member"];
            $tax_rate = $row["tax_rate"];
            $tax_inc = $row["tax_inc"];
            $postage_method = $row["postage_method"];
            $warning_level = $row["warning_level"];
            $sell_into_minus = $row["sell_into_minus_stock"];
            $license = $row["license"];
            $download_dir = $row["download_dir"];
            $store_address1 = $row["store_address1"];
            $store_address2 = $row["store_address2"];
            $store_address3 = $row["store_address3"];
            $store_address4 = $row["store_address4"];
            $thumb_height = $row["thumbnail_size_height"];
            $thumb_size = $row["thumbnail_size"];


            if($use_coupons==1)
            {
                $ischecked = "checked";
            }
            if($customer_member=="yes")
            {
                $ischecked2 = "checked";
            }
            if($tax_inc==1)
            {
                $ischecked3="checked";
            }
            if($sell_into_minus==1)
            {
                $ischecked4="checked";
            }
    
        }
        $formaction = 1;
    }
    else if($action==2)
    {
        
    }
    else
    {
        $formaction = 2;
    }

    $display .= "<center><form action='".e_SELF."' method='POST' name='settingsform' ID='settingsform'>
                <br />
                <table width='95%' class='fborder'>
                    <tr>    
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_name.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='store_name' value='".$store_name."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_order_email.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='order_email' value='".$order_email."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_use_coupons.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='use_coupons' ".$ischecked.">
                            &nbsp;&nbsp;".lan_admin_use_coupons_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_currency.": 
                        </td>
                        <td class='forumheader3'>
                            <input type=\"text\" class=\"tbox\" name='store_currency' value='".$store_currency."'>
                            &nbsp;&nbsp;".lan_admin_store_currency_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_cur_symbol.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='currency_symbol' value='".$currency_symbol."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_order_id.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='store_order_id' value='".$store_order_id."'>
                            &nbsp;&nbsp;".lan_admin_store_order_id_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_products_order.": 
                        </td>
                        <td class='forumheader3'>
                            ".e107shop_get_product_field_list($item_display_order)."
                            &nbsp;&nbsp;".lan_admin_store_products_order_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_products_perpage.": 
                        </td>
                        <td class='forumheader3'>
                            <input type=\"text\" class=\"tbox\" name=\"per_page\" value='".$products_per_page."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_cat_display.": 
                        </td>
                        <td class='forumheader3'>
                            ".e107shop_admin_get_cat_display($category_display)."
                            &nbsp;&nbsp;".lan_admin_store_cat_display_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_customer_member.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='customer_member' ".$ischecked2.">
                            &nbsp;&nbsp;".lan_admin_customer_member_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_tax_inc.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='tax_inc' ".$ischecked3.">
                            &nbsp;&nbsp;".lan_admin_tax_inc_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_tax_rate.": 
                        </td>
                        <td class='forumheader3'>
                            <input type=\"text\" class=\"tbox\" name=\"tax_rate\" value='".$tax_rate."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_post_method.":
                        </td>
                        <td class='forumheader3'>
                            ".e107shop_get_postage_method_list($postage_method)."
                        </td>
                    </tr>
                        <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_store_warning.": 
                        </td>
                        <td class='forumheader3'>
                            <input type=\"text\" class=\"tbox\" name=\"warning_level\" value='".$warning_level."'>
                            &nbsp;&nbsp;".lan_admin_store_warning_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_sell_minus_stock.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='minus_stock' ".$ischecked4.">
                            &nbsp;&nbsp;".lan_sell_minus_stock_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_business_details1.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='store_address1' value='".$store_address1."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_business_details2.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='store_address2' value='".$store_address2."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_business_details3.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='store_address3' value='".$store_address3."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_business_details4.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='store_address4' value='".$store_address4."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_license.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='license' value='".$license."'>
                            &nbsp;&nbsp;".lan_admin_license_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_thumb_size.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' size='3' name='thumbnail_size' value='".$thumb_size."'>
                            &nbsp;&nbsp;".lan_admin_thumb_size_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_download_dir.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='download_dir' value='".$download_dir."'>
                            &nbsp;&nbsp;".lan_admin_download_dir_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader' colspan='2'>
                            <input class=\"tbox\" type='hidden' value='".$formaction."' name='faction'>
                            <center><input class=\"tbox\" type='submit' value='".lan_global_update."' name='subbed'></center>
                        </td>
                    </tr>

                    </form>
                    </table>";
                    $ns->tablerender("<center>".lan_admin_menu_settings."</center>", $display);
}

/*
    Function : e107shop_admin_process_category
    -------------------------------------------------
    Parameters : todo yet
    =================================================
    Processes the submitted details into the database
    =================================================
*/

function e107shop_admin_process_settings($action, $store_name, $order_email, $use_coupons, 
$store_currency, $currency_symbol, $store_order_id, $product_order, $per_page, $cat_display, 
$customer_member, $tax_inc, $tax_rate, $postage_method, $warning_level, $minus_stock, $license, 
$download_dir, $store_address1, $store_address2, $store_address3, $store_address4, $thumb_size) {
    global $e107shop_db_name, $sql, $ns, $tp;
    if($use_coupons!="") {
        $use_coupons=1;
    } else {
        $use_coupons=2;
    }

    if($customer_member!="") {
        $customer_member="yes";
    } else {
        $customer_member=2;
    }

    if($minus_stock!="") {
        $minus_stock=1;
    } else {
        $minus_stock=2;
    }

    if($tax_inc!="") {
        $tax_inc=1;
    } else {
        $tax_inc=2;
    }
    if($action==1) {
        //
        // Change admin settings
        //
        $sql->db_Update($e107shop_db_name['settings'], "store_name='".$tp->toDB($store_name)."', postage_method='".
        $postage_method."', currency='".$store_currency."', currency_symbol='".
        str_replace (" ", "&nbsp;", htmlentities($currency_symbol, ENT_NOQUOTES, "UTF-8"))
        ."', use_coupons='".$use_coupons."', orderID=".
        $store_order_id.", order_email='".$order_email."', tax_inc='".
        $tax_inc."', tax_rate='".$tax_rate."', warning_level='".
        $warning_level."', item_display_order='".$product_order."', category_display='".
        $cat_display."', products_per_page='".$per_page."', customer_must_be_member='".
        $customer_member."', sell_into_minus_stock='".$minus_stock."', license='".
        $license."', download_dir='".$download_dir."', store_address1='".
        $store_address1."', store_address2='".$store_address2."', store_address3='".
        $store_address3."', store_address4='".$store_address4."', thumbnail_size='".$thumb_size."'");

        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center>".lan_global_updated."</center>");
    }
}

/*
    Function : e107shop_get_product_field_list
    ------------------------------------------
    Parameters : $current   current product
    ==========================================
    A list of current languages
    ==========================================
*/

function e107shop_get_product_field_list($current) {
  $display = "<select class='tbox' name='product_order'>";
     if($current=='product_price')
  {
    $display .= "<option selected value='product_price'>".lan_admin_sort_product_price."</option>
                 <option value='product_name'>".lan_admin_sort_product_name."</option>
                 <option value='product_tier'>".lan_admin_sort_product_tier."</option>
                 <option value='date_added'>".lan_admin_sort_product_date_added."</option>";
  }
  else if($current=='product_name')
  {
    $display .= "<option value='product_price'>".lan_admin_sort_product_price."</option>
              <option selected value='product_name'>".lan_admin_sort_product_name."</option>
              <option value='product_tier'>".lan_admin_sort_product_tier."</option>
              <option value='date_added'>".lan_admin_sort_product_date_added."</option>";
  }
  else if($current=='product_tier')
  {
    $display .= "<option value='product_price'>".lan_admin_sort_product_price."</option>
              <option value='product_name'>".lan_admin_sort_product_name."</option>
              <option selected value='product_tier'>".lan_admin_sort_product_tier."</option>
              <option value='date_added'>".lan_admin_sort_product_date_added."</option>";
  }
  else if($current=='date_added')
  {
    $display .= "<option value='product_price'>".lan_admin_sort_product_price."</option>
              <option value='product_name'>".lan_admin_sort_product_name."</option>
              <option value='product_tier'>".lan_admin_sort_product_tier."</option>
              <option selected value='date_added'>".lan_admin_sort_product_date_added."</option>";
  }
  else
      $display .= "<option selected value='product_price'>".lan_admin_sort_product_price."</option>
              <option value='product_name'>".lan_admin_sort_product_name."</option>
              <option value='product_tier'>".lan_admin_sort_product_tier."</option>
              <option value='date_added'>".lan_admin_sort_product_date_added."</option>";
    $display .= "</select>";
            return $display;
    }  
  
/*  COMMENTED OUT FOR LATER USE

    global $sql;
    $display = "<select name='product_order' class='tbox'>";
    $query = "SHOW COLUMNS FROM ".MPREFIX.$e107shop_db_name['products'];
    $sql->db_Select_gen($query);
    while($row=$sql->db_Fetch())
    {
        if($row[0]==$current)
        {
            $display.="<option selected value='".$row[0]."'>".$row[0]."</option>";
        }
        else
        {
            $display.="<option value='".$row[0]."'>".$row[0]."</option>";
        }
    }

    $display .="</select>";

    return $display;
*/

/*
    Function : e107shop_admin_get_cat_display
    -----------------------------------------
    Parameters : $category_display
    =========================================
    Drop Down list of display for category
    =========================================
*/

function e107shop_admin_get_cat_display($category_display) {
    global $e107shop_db_name, $sql;
    $display="<select name='cat_display' class='tbox'>";
    if($category_display=='1')
    {
        $display.="<option value='1' selected>List Style</option>
                   <option value='2'>Catagory Style</option>";
    }
    else
    {
        $display.="<option value='2' selected>Category Style</option>
                   <option value='1'>List Style</option>";
    }
    $display.="</select>";

    return $display;
}

/*
    Function : e107shop_get_postage_method_list
    -----------------------------------------------
    Parameters : $postage_method    The post method
    ===============================================
    Drop Down list of postage methods
    ===============================================
*/

function e107shop_get_postage_method_list($postage_method) {
        global $e107shop_db_name, $sql;
    $display="<select name='postage_method' class='tbox'>";
    if($postage_method=='1')
    {
        $display.="<option value='1' selected>".lan_admin_post_included."</option>
                   <option value='2'>".lan_admin_post_peritem."</option>
                   <option value='3'>".lan_admin_post_region."</option>";
    }
    else if($postage_method==2)
    {
        $display.="<option value='1'>".lan_admin_post_included."</option>
                   <option value='2' selected>".lan_admin_post_peritem."</option>
                   <option value='3'>".lan_admin_post_region."</option>";
    }
    else if($postage_method==3)
    {
        $display.="<option value='1'>".lan_admin_post_included."</option>
                   <option value='2'>".lan_admin_post_peritem."</option>
                   <option value='3' selected>".lan_admin_post_region."</option>";
    }
    else
    {
        $display.="<option value='1'>".lan_admin_post_included."</option>
                   <option value='2'>".lan_admin_post_peritem."</option>
                   <option value='3'>".lan_admin_post_region."</option>";
    }

    $display.="</select>";

    return $display;
}
